﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Medianamik.Core;
using Medianamik.Core.DAL;
using Medianamik.Core.DAL.Repositories;
using Medianamik.Core.DAL.Services;
using SubSonic;

namespace Medianamik.UI.Web.Shop.Data.Repositories
{
    public class ProductGroupedRepository<TCollection, TItem> : MedianamikRepository<TCollection, TItem>, IProductGroupedRepository<TItem>
        where TItem : Interfaces.IProduct, IRecordBase, new()
        where TCollection : ICollectionWithChildren<TCollection, TItem>, new()
    {
        public ProductGroupedRepository(ICultureService cultureService, 
            IContentStateService contentStateService, INodeMapper nodeMapper) 
            : base(cultureService, contentStateService, nodeMapper)
        {
        }

        public PagedCollection<TItem> PagedWithFilters(int pageIndex, int pageSize, 
            bool? IsActive, Guid? ParentNodeId, Guid? TypeId, Guid? Brand, Guid? Categories, string keywords)
        {

            throw new NotImplementedException();
        }

        public int GetProductCount(bool? isActive, Guid? parentNodeId, Guid? typeId, Guid? brand, Guid? categories, string keywords)
        {

            using (var reader = SPs.GetGroupedProduct(isActive, parentNodeId,typeId, brand, categories, String.IsNullOrEmpty(keywords)
                                        ? null
                                        : String.Concat("%", keywords, "%")).GetReader())

            {
                if (reader.Read())
                    return reader.GetInt32(0);
            }

            throw new Exception("Problemo");
        }
    }


    public interface IProductGroupedRepository<TProduct> : IMedianamikRepository<TProduct>
    where TProduct : Interfaces.IProduct, IRecordBase, new()
    {
        PagedCollection<TProduct> PagedWithFilters(int pageIndex, int pageSize,
            bool? isActive, Guid? parentNodeId, Guid? typeId, Guid? brand, Guid? categories, string keywords);

        int GetProductCount(bool? isActive, Guid? parentNodeId, Guid? typeId, Guid? brand, Guid? categories, string keywords);
    }
}
